<template>
  <view class="nav-bar">
    <image :src="backgroundImage" class="bg-image"></image>
    <view class="title-bar" :style="{ paddingTop: statusBarHeight + 'px' }">
      <view class="back-arrow" @click="handleBack"></view>
      <view class="title-text">{{ title }}</view>
      <view class="right-placeholder"></view>
    </view>
  </view>
</template>

<script setup>
import { defineProps, defineEmits, ref, onMounted } from 'vue'

const props = defineProps({
  title: {
    type: String,
    default: '页面标题'
  },
  backgroundImage: {
    type: String,
    default: '/static/image/图层 1.png'
  }
})

const emit = defineEmits(['back'])
const statusBarHeight = ref(0)

const handleBack = () => {
  emit('back')
}

// 获取状态栏高度
onMounted(() => {
  try {
    // 使用推荐的wx.getWindowInfo API获取状态栏高度，避免废弃警告
    const windowInfo = uni.getWindowInfo();
    statusBarHeight.value = windowInfo.statusBarHeight;
  } catch (error) {
    console.error('获取窗口信息失败:', error);
    // 降级处理：设置默认值
    statusBarHeight.value = 0;
  }
})
</script>

<style scoped>
.nav-bar {
  width: 100%;
  position: fixed;
  z-index: -1;
  top: 0;
}

.bg-image {
  width: 100%;
  height: 200px;
  position: fixed;
  z-index: 0;
}

.title-bar {
  padding: 0 15px 15px 15px;
  width: 100%;
  display: flex;
  box-sizing: border-box;
  position: fixed;
  z-index: 1000;
  justify-content: space-between;
  align-items: center;
  min-height: 44px;
}

.back-arrow {
  width: 24px;
  height: 24px;
  position: relative;
  cursor: pointer;
  transition: all 0.3s ease;
}

.back-arrow::before {
  content: "";
  position: absolute;
  width: 12px;
  height: 12px;
  border-top: 2px solid #fff;
  border-left: 2px solid #fff;
  transform: rotate(-45deg);
  top: 50%;
  left: 0;
  margin-top: -6px;
  transition: all 0.3s ease;
}

.back-arrow:hover::before {
  border-color: #007AFF;
  transform: rotate(-45deg) scale(1.1);
}

.title-text {
  color: #fff;
  font-size: 18px;
  font-weight: 500;
}

.right-placeholder {
  width: 24px;
  height: 24px;
}
</style>